﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.pproProductType
{
    public partial class Edit : TreeEditPagebase<IProductTypeContract, ProductType>
    {

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string PageCode
        {
            get
            {
                return "sysDictionary";
            }
        }

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "sysDictionaryview";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var TreeList = FineUITreeHelper.ResolveDDL<ProductType>(Manager.GetResolvedTree(), EntityId);
                FineUIListHelper.BindTreeDDL<ProductType>(TreeList, 0L, ed_Parent);
                if (PageMode != pagemode.Add)
                    LoadData();
                else
                    GetSortIndex();
            }
        }

        protected override void LoadData(ProductType entity)
        {
            ed_Code.SetValue(entity.Code);
            ed_Name.SetValue(entity.Name);
            ed_SortIndex.SetValue(entity.SortIndex);
            ed_Remark.SetValue(entity.Remark);
            if(!entity.Parent.IsNull())
                ed_Parent.SetValue(entity.Parent.Id);

        }

        protected override OperationResult SaveData(ProductType entity)
        {
            entity.Code = ed_Code.GetValue();
            entity.Name = ed_Name.GetValue();
            entity.SortIndex = ed_SortIndex.GetValue().ToInt();
            entity.Remark = ed_Remark.GetValue();

            if (ed_Parent.GetValue().ToLong() == 0L)
            {
                entity.ParentId = null;
            }
            else
            {
                entity.ParentId = ed_Parent.GetValue().ToLong();
            }
            return base.SaveData(entity);
            
        }
    }
}
